package pogvue.gui;

import pogvue.datamodel.SequenceI;
import pogvue.gui.event.AlignViewportEvent;
import pogvue.gui.event.SequenceSelectionEvent;
import pogvue.gui.event.StatusEvent;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

public final class IdPanel extends JPanel implements MouseListener, MouseMotionListener {

  public IdCanvas       idCanvas;

  protected int            offy;
  public    int            width;
  private int            lastid;

  private final AlignViewport  av;
  private final Controller     controller;

  private boolean mouseDown;
  private boolean mouseUp;

  public IdPanel(AlignViewport av,Controller c) {
    this.av         = av;
    this.controller = c;

    componentInit();
    addMouseListener(this);
    addMouseMotionListener(this);
  }

  private void componentInit() {
    idCanvas = new IdCanvas(av,controller);

    setLayout(new BorderLayout());
    add("Center",idCanvas);

    idCanvas.addMouseListener(this);
    idCanvas.addMouseMotionListener(this);
  }

    public Dimension getPreferredSize() {
	return idCanvas.getPreferredSize();
    }
    
    public void mouseMoved(MouseEvent e) {}

  private void selectSeqs(int start, int end) {
     if (end < start) {
       int tmp = start;
       start = end;
       end   = tmp;
     }

     for (int i = start; i <= end; i++) {
       SequenceI pickedSeq = av.getAlignment().getSequenceAt(i);
       controller.handleStatusEvent(new StatusEvent(this,"Sequence ID : " + pickedSeq.getName() +  " (" + i + ")", StatusEvent.INFO));


       if (av.getSelection().contains(pickedSeq)) {
         av.getSelection().removeElement(pickedSeq);
         fireSequenceSelectionEvent(av.getSelection());
       } else {
         av.getSelection().addElement(pickedSeq);
         fireSequenceSelectionEvent(av.getSelection());
       }
       controller.handleAlignViewportEvent(new AlignViewportEvent(this,av,AlignViewportEvent.SHOW));
     } 

   }

  public void mouseDragged(MouseEvent e) {
    int y = e.getY();

    int seq = av.getIndex(y);

    if (mouseDown) {
      if (seq < lastid) {
        selectSeqs(lastid-1,seq);
      } else if (seq > lastid) {
         selectSeqs(lastid+1,seq);
      }
      lastid = seq;
    }

  }
      
  public void mouseClicked(MouseEvent e) { }
  public void mouseEntered(MouseEvent e) { }
  public void mouseExited (MouseEvent e) { }

  private void fireSequenceSelectionEvent(Selection sel) {
    controller.handleSequenceSelectionEvent(new SequenceSelectionEvent(this,sel));
  }

  public void mousePressed(MouseEvent e) {
    int x = e.getX();
    int y = e.getY();

    int seq = av.getIndex(y);

    mouseDown = true;

      if ((e.getModifiers() & Event.META_MASK) != 0) {
        try {	
          if (seq != -1) {
            String id = av.getAlignment().getSequenceAt(seq).getName();
            if ( id.indexOf("/") != -1) {
              id = id.substring(0,id.indexOf("/"));
            }
          }
      	} catch (Exception ex) {
      	  System.out.println("Exception : " + ex);
      	}
      } else {
 
        if (seq != -1) {
          selectSeqs(seq,seq);
        }

        lastid = seq;
      }
  }

  public void mouseReleased(MouseEvent e) {
    lastid = -1;

    mouseDown = false;
    mouseUp   = true;

  }
}
